﻿@using Ornament.MVCWebFrame.Areas.MemberShips.Models
@using Ornament.MemberShip.Permissions
@using Ornament.Web
@using Qi
@model PermissionWizard
@{
    Layout = "~/Views/Shared/App.cshtml";
    Permission permission = Model.GetPermission();
    var type = OrnamentContext.GetOperatorType(permission.Resource);
    var operatorKeyMaping = EnumHelper.GetDescriptionList(type);
}
<form method="post" action="@Url.Action("Save")">
<fieldset id="editPanel" class="cssform">
    <legend>许可证信息</legend>
    
    @Html.Partial("PermissionWizardDataView", this.Model)
    <p>
        <label>
            @Html.GetResourceString("Basic", "Name"):
        </label>
        @Html.TextBox("PermissionName", permission.Name)
    </p>
    <p>
        <label>
            @Html.GetResourceString("Basic", "Comment"):
        </label>
        @Html.TextArea("PermissionComment", permission.Remark)
    </p>
    <div class="fieldItem">
        <label>
            操作:</label>
        <div id="OperatorsList" class="data" style="width: 18em">
            @foreach (string key in operatorKeyMaping.Keys)
            {
                <input type="checkbox" id="@operatorKeyMaping[key]" value="@Convert.ToInt32(operatorKeyMaping[key])" name="PermissionOperatorValue" />
                <label for="@operatorKeyMaping[key]">
                    @key</label>
                <br />
            }
        </div>
    </div>
</fieldset>
<div>
    <button id="previous" type="submit" value="false" name="forward">@Resources.Basic.Previous
    </button>
    <button id="save" type="submit" value="true" name="forward">
        @Resources.Basic.Finish</button>
    <button id="cancel" type="button">
        @Resources.Basic.Cancel</button>
</div>
</form>
@section header
{
    <style type="text/css">
        #editPanel
        {
            width: 100%;
        }
        /* clear cssform label effective*/#OperatorsList label
        {
            float: none;
            margin-right: 0em;
            text-align: left;
            width: 4em;
        }
    </style>
    <script type="text/javascript">

        $(document).ready(function () {
            $("#save").button().click(function () {
                $("#operatorValue").val(getOperatorValue());

            });


            $("#OperatorsList :checkbox").click(function () {

                var check = this.checked;
                var beCheckValue = $(this).val();

                $(this).siblings(":checkbox").each(function () {
                    if (beCheckValue == 0) {
                        this.checked = !check;
                    }
                    var checkValue = $(this).val();
                    var include;
                    if (beCheckValue >= checkValue) {
                        include = hasPermission(beCheckValue, checkValue);
                        if (include && check) {
                            this.checked = true;
                        }
                    } else {
                        include = hasPermission(checkValue, beCheckValue);
                        if (include && !check) {
                            this.checked = false;
                        }
                    }
                });
            });

            SetOperatorsValue("@permission.Operator");


        });


        function getOperatorValue() {
            var operator = 0;
            $("#OperatorsList :checkbox").each(function () {
                if (this.checked) {
                    operator |= parseInt($(this).val());
                }
            });
            return operator;
        }

        function SetOperatorsValue(permissions) {
            $("#OperatorsList :checkbox").each(function () {
                var theValue = parseInt(this.value);
                var value = theValue & permissions;
                this.checked = (value == theValue) && (permissions > theValue) && (theValue != 0);
            });
        }  
    </script>
}
